//
//  LoginViewController.m
//  mytext
//
//  Created by wj on 16/6/6.
//  Copyright © 2016年 wj. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "ForgetterViewController.h"
#import "BaseTabBarVC.h"
#import "LogFirstTimeViewController.h"
#import "registerPayViewController.h"
#import "writePInfoViewController.h"
@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UILabel *accountLabel;
@property (weak, nonatomic) IBOutlet UILabel *sucrityLabel;
@property (nonatomic,strong) CFinfoModel * model;
@property (nonatomic,strong) NSMutableDictionary * mydic;
@property (strong,nonatomic) NSDictionary * myBigDic;

@end

@implementation LoginViewController
-(NSDictionary *)myBigDic
{
    if (!_myBigDic) {
        _myBigDic = [NSDictionary new];
        
    }
    return _myBigDic;
}

-(NSMutableDictionary *)mydic
{
    if (!_mydic) {
        self.model = [CFinfoModel new];
        _mydic = [NSMutableDictionary new];
        _mydic = [self.model readTheNetUserDic];

    }
    return _mydic;
}

-(void)viewWillAppear:(BOOL)animated
{
    if ([Tools getValueFromNSUserDefaultsByKey:@"userName"])
    {
        self.userNameT.text = [Tools getValueFromNSUserDefaultsByKey:@"userName"];
    }
    if ([Tools getValueFromNSUserDefaultsByKey:@"password"])
    {
        self.passwordT.text = [Tools getValueFromNSUserDefaultsByKey:@"password"];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self setUI];
    
}

- (void)setUI
{
    self.accountLabel.layer.cornerRadius=5;
    self.accountLabel.layer.borderWidth = 1;
    self.accountLabel.layer.borderColor = RGB(227, 227, 227).CGColor;
    
    self.sucrityLabel.layer.cornerRadius = 4;
    self.sucrityLabel.layer.borderWidth = 1;
    self.sucrityLabel.layer.borderColor = RGB(227, 227, 227).CGColor;
    
    self.title = @"登录";
    self.loginBtn.layer.cornerRadius = 5;
    
    UIView *leftV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 11, 1)];
    self.userNameT.leftView = leftV;
    self.userNameT.leftViewMode = UITextFieldViewModeAlways;
    
    UIView *leftV3 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 11, 1)];
    self.passwordT.leftView = leftV3;
    

}

- (IBAction)forgetPassWord:(id)sender
{
    ForgetterViewController *forgetVC = [[ForgetterViewController alloc] init];
    UINavigationController *forgetNav = [[UINavigationController alloc]initWithRootViewController:forgetVC];
    
    [self presentViewController:forgetNav animated:YES completion:nil];
}


- (IBAction)login:(id)sender
{


    [self loginWithUserName:self.userNameT.text passWord:self.passwordT.text];
    
}

#pragma mark ------ 跳转注册

- (IBAction)newRegister:(id)sender
{
    RegisterViewController *registerVC = [[RegisterViewController alloc] init];
    UINavigationController *registerNav = [[UINavigationController alloc]initWithRootViewController:registerVC];
    [self presentViewController:registerNav animated:YES completion:nil];
}

#pragma mark - 登录
- (void)loginWithUserName:(NSString *)userName passWord:(NSString *)password
{
    //    mobile = 13622355513;
    //    power = 0;
    //    "role_id" = 1;
    //    username = shoper;
    
    NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
    
    [dictM setValue:userName forKey:@"mobile"];
    [dictM setValue:password forKey:@"password"];
    NSLog(@"%@",self.mydic);
    if ([self.mydic  isEqual: @{}]) {
         NSString * uuid = [[NSUUID UUID] UUIDString];
        [dictM setValue:uuid forKey:@"sessionkey"];
    }else{
    [dictM setValue:self.mydic[@"sessionkey"] forKey:@"sessionkey"];
    }

    NSLog(@"_mydic ==== %@",self.mydic);
    NSLog(@"%@",self.mydic[@"sessionkey"]);
    
    [[LJFNetworkingManager shareManager] POST:LOGINURL parameters:dictM success:^(id returnData, int code, NSString *msg)
     {
         if (code == 1||code ==2)
         {
             NSLog(@"%d",[returnData[@"status"] intValue]);
    
             
             NSLog(@"msg == %@",msg);
             [self.model saveTheNetDic:returnData];
             self.mydic = [self.model readTheNetUserDic];
             [Tools syncNSUserDeafaultsByKey:USERINFO withValue:self.mydic];
             [Tools syncNSUserDeafaultsByKey:USERID withValue:self.mydic[@"id"]];
             [Tools syncNSUserDeafaultsByKey:@"userName" withValue:self.mydic[@"mobile"]];
             [Tools syncNSUserDeafaultsByKey:@"password" withValue:self.mydic[@"password"]];
             [Tools syncNSUserDeafaultsByKey:@"sessionkey" withValue:self.mydic[@"sessionkey"]];

             [self requestTheNetWith:nil andURL:MYALLINFO_URL andDic:@{@"id":[Tools getValueFromNSUserDefaultsByKey:USERID]}];
             
  
         }else
         {
             NSLog(@"msg == %@",msg);
             [SVProgressHUD showErrorWithStatus:msg];
         }
     } failure:^(NSError *error)
     {
         
     } showHUD:self.view];
    
}
#pragma mark --------------网络请求
-(void)requestTheNetWith:(NSArray *)myRequestArry andURL:(NSString *)myurl andDic:(NSDictionary *)myDic;
{
    if (myRequestArry.count>0) {
        [[LJFNetworkingManager shareManager] POST:myurl upLoadImages:myRequestArry parameters:myDic success:^(id returnData, int code, NSString *msg) {
            NSLog(@"%@",msg);
           
        } failure:^(NSError *error) {
            NSLog(@"失败");
          
        } showHUD:self.view];
        
    }else
    {
        [[LJFNetworkingManager shareManager] POST:myurl parameters:myDic success:^(id returnData, int code, NSString *msg) {
            NSLog(@"%@",msg);
            NSLog(@"returnData ====   %@",returnData);
            [self.model saveTheBiggestDic:returnData];
            ///这里应该写个判断 判断用户是否是已经完善了自己的个人信息，如果已经完善了 直接跳去tabcontrol ， 如果未完善跳去完善页面再跳tabbarcontrol
        
            if ([returnData[@"user"][SEX_KEY] isKindOfClass:[NSNull class]]||[self.mydic[@"userName"] isEqualToString:@""]) {
                
                writePInfoViewController *writeInfoVC = [[UIStoryboard storyboardWithName:@"writePInfoViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"writePInfoViewController"];
                 UINavigationController *writeNav = [[UINavigationController alloc]initWithRootViewController:writeInfoVC];
                [self presentViewController:writeNav animated:YES completion:nil];
                
                return ;
            }
            if ([returnData[@"user"][@"status"] intValue] == 0) {
                registerPayViewController *registerVC = [[UIStoryboard storyboardWithName:@"registerPayViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"registerPayViewController"];
                [self presentViewController:registerVC animated:YES completion:nil];
            }
            if (self.mydic[@"homeMarryTime"]!=nil&& ![self.mydic[@"homeMarryTime"] isKindOfClass:[NSNull class]]&&![self.mydic[@"homeMarryTime"] isEqualToString:@""]) {
                BaseTabBarVC *baseVC = [[BaseTabBarVC alloc]init];
                [self presentViewController:baseVC animated:YES completion:nil];
            }else{
                LogFirstTimeViewController * lgFTVC = [[UIStoryboard storyboardWithName:@"FirstSearchViewController" bundle:nil ] instantiateViewControllerWithIdentifier:@"LogFirstTimeViewController"];
                [self presentViewController:lgFTVC animated:YES completion:nil];
            }
        } failure:^(NSError *error) {
            NSLog(@"%@",error);
        } showHUD:self.view];
        
    }
    
    
}

@end
